package com.example.qianjun.robotbetago;

import android.graphics.Point;

/**
 * Created by qianjun on 2016/5/23.
 */
public class Robot {
    //机器人方向
    //0:右   1：下   2：左   3：上
    private static int direct = 0;
    private static Point point = new Point(3, 2);
    public static char dirChar = '→';

    public static void setDirChar(char dirChar) {
        Robot.dirChar = dirChar;
    }

    public static int getDirect() {
        return direct;
    }

    public static void setDirect(int direct1) {
        direct = direct1;
    }

    public static Point getPoint() {
        return point;
    }

    public static void setPoint(Point point1) {
        point = point1;
    }

    public static void goForSteps(int steps) {
        for (int i = 0; i < steps; i++) {
            goForOneStep();
        }
    }

    public static void turnRignt() {
        switch (direct) {
            case 0:
                direct = 1;

                break;
            case 1:
                direct = 2;

                break;
            case 2:
                direct = 3;

                break;
            case 3:
                direct = 0;

                break;
        }

        dirCharChange();
    }

    public static void dirCharChange() {
        switch (direct) {
            case 0:
                dirChar = '→';
                break;
            case 1:
                dirChar = '↓';
                break;
            case 2:
                dirChar = '←';
                break;
            case 3:
                dirChar = '↑';
                break;
        }
    }

    public static void turnLeft() {
        switch (direct) {
            case 0:
                direct = 3;
                break;
            case 1:
                direct = 0;
                break;
            case 2:
                direct = 1;
                break;
            case 3:
                direct = 2;
                break;
        }

        dirCharChange();
    }

    public static void turnBack() {
        switch (direct) {
            case 0:
                direct = 2;
                break;
            case 1:
                direct = 3;
                break;
            case 2:
                direct = 0;
                break;
            case 3:
                direct = 1;
                break;
        }

        dirCharChange();
    }

    public static void goForOneStep() {

        switch (direct) {
            case 0:
                if (point.x < 7) {

                    moveStep(point.x + 1, point.y);

                    CheckBoard.table[point.x][point.y] = '#';
                    point.set(point.x + 1, point.y);


                }

                break;
            case 1:
                if (point.y < 7) {

                    moveStep(point.x, point.y + 1);

                    CheckBoard.table[point.x][point.y] = '#';
                    point.set(point.x, point.y + 1);


                }
                break;
            case 2:

                if (point.x > 0) {

                    moveStep(point.x - 1, point.y);

                    CheckBoard.table[point.x][point.y] = '#';
                    point.set(point.x - 1, point.y);


                }

                break;
            case 3:

                if (point.y > 0) {

                    moveStep(point.x, point.y - 1);

                    CheckBoard.table[point.x][point.y] = '#';
                    point.set(point.x, point.y - 1);

                }

                break;
        }


    }

    //机器人前方的字母走向
    private static void moveStep(int x, int y) {
        int x1 = x;
        int y1 = y;

        if (CheckBoard.table[x1][y1] == '#') {

            return;
        }


        switch (direct) {
            case 0:
                if (x1 + 1 > 7) {
                    CheckBoard.table[x1][y1] = '#';
                    return;
                }
                if (CheckBoard.table[x1 + 1][y1] != '#') {
                    moveStep(x1 + 1, y1);
                    if (CheckBoard.table[x1 + 1][y1] == '#') {
                        CheckBoard.table[x1 + 1][y1] = CheckBoard.table[x1][y1];
                        CheckBoard.table[x1][y1] = '#';
                    }
                } else {
                    CheckBoard.table[x1 + 1][y1] = CheckBoard.table[x1][y1];
                    CheckBoard.table[x1][y1] = '#';
                }
                break;
            case 1:
                if (y1 + 1 > 7) {
                    CheckBoard.table[x1][y1] = '#';
                    return;
                }

                if (CheckBoard.table[x1][y1 + 1] != '#') {
                    moveStep(x1, y1 + 1);
                    if (CheckBoard.table[x1][y1 + 1] == '#') {
                        CheckBoard.table[x1][y1 + 1] = CheckBoard.table[x1][y1];
                        CheckBoard.table[x1][y1] = '#';
                    }
                } else {
                    CheckBoard.table[x1][y1 + 1] = CheckBoard.table[x1][y1];
                    CheckBoard.table[x1][y1] = '#';
                }
                break;
            case 2:

                if (x1 - 1 < 0) {
                    CheckBoard.table[x1][y1] = '#';
                    return;
                }

                if (CheckBoard.table[x1 - 1][y1] != '#') {
                    moveStep(x1 - 1, y1);
                    if (CheckBoard.table[x1 - 1][y1] == '#') {
                        CheckBoard.table[x1 - 1][y1] = CheckBoard.table[x1][y1];
                        CheckBoard.table[x1][y1] = '#';
                    }
                } else {
                    CheckBoard.table[x1 - 1][y1] = CheckBoard.table[x1][y1];
                    CheckBoard.table[x1][y1] = '#';
                }
                break;
            case 3:
                if (y1 - 1 < 0) {
                    CheckBoard.table[x1][y1] = '#';
                    return;
                }
                if (CheckBoard.table[x1][y1 - 1] != '#') {
                    moveStep(x1, y1 - 1);
                    if (CheckBoard.table[x1][y1 - 1] == '#') {
                        CheckBoard.table[x1][y1 - 1] = CheckBoard.table[x1][y1];
                        CheckBoard.table[x1][y1] = '#';
                    }
                } else {
                    CheckBoard.table[x1][y1 - 1] = CheckBoard.table[x1][y1];
                    CheckBoard.table[x1][y1] = '#';
                }
                break;

        }


    }
}
